package net.jielan188.hd.util;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具
 * Created by hgg on 2017/1/12.
 */
public class CommonUtils {

    /**
     * 首字母大写
     * @param name
     * @return
     */
    public static String firstNameToLarge(String name ){

        try {
            if( StringUtils.isBlank( name ) ){
                return null;
            }

            return name.substring(0,1).toUpperCase()+name.substring(1);
        }catch ( Exception e ){
            return null;
        }
    }

    /**
     * 首字母小写
     * @param name
     * @return
     */
    public static String firstNameToSmall(String name ){

        try {
            if( StringUtils.isBlank( name ) ){
                return null;
            }

            return name.substring(0,1).toLowerCase()+name.substring(1);
        }catch ( Exception e ){
            return null;
        }
    }

    /**
     * 大写转下划线的算法 驼峰转 下划线
     * @param param
     * @return
     */
    public static String camel4underline(String param ){

        Pattern pattern = Pattern.compile("[A-Z]");
        if( param == null ||param.equals("") ){
            return "";
        }
        StringBuilder builder = new StringBuilder( param );
        Matcher mc = pattern.matcher( param );
        int i=0;
        while( mc.find() ){
            builder.replace( mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase() );
            i++;
        }
        if( '_' == builder.charAt( 0 ) ){
            builder.deleteCharAt( 0 );
        }
        return builder.toString();
    }
}
